﻿/*************************************************************
 * Software of blackcore
 *
 * CLRVersion:4.0.30319.586
 * MachineName:SPACE
 * Author:Andy Huang
 * Email:itblackhole@gmail.com
 * DateTime：6/26/2013 1:58:41 PM
 * Function：
 *************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using SobFS.Common.Database;
using MySql.Data.MySqlClient;

namespace SobFS.MySQLDAL
{
    public partial class SysUser
    {
        #region ******************************Private Property******************************
        #endregion ******************************Private Property******************************


        #region ******************************Public Property******************************
        #endregion ******************************Public Property******************************


        #region ******************************Structure******************************
        #endregion  ******************************Structure******************************


        #region ******************************Private Method******************************
        #endregion ******************************Private Method******************************


        #region ******************************Public Method******************************
        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM SysUser");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" WHERE " + strWhere);
            }
            recordCount = Convert.ToInt32(DbHelperMySQL.GetSingle(PagingHelper.CreateCountingSql(strSql.ToString())));
            return DbHelperMySQL.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, strSql.ToString(), filedOrder));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="loginInfo">可以是LoginName或Email</param>
        /// <param name="password"></param>
        /// <returns></returns>
        public bool Sign(string loginInfo, string password, out Model.SysUser model)
        {
            bool result = false;
            model = null;

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,OrganizationId,LoginName,Password,RealName,NickName,Email,Mobile,QQ,Blog,Weibo,IsObsoleted,IsEnabled,Description from SysUser ");
            strSql.Append("WHERE (LoginName=@LoginName OR Email=@Email) AND Password=@Password AND IsObsoleted = 0 AND IsEnabled =1");
            MySqlParameter[] parameters = {
			        new MySqlParameter("@LoginName", MySqlDbType.VarChar,50),
                    new MySqlParameter("@Email", MySqlDbType.VarChar,100),
					new MySqlParameter("@Password", MySqlDbType.VarChar,100)
            };
            parameters[0].Value = loginInfo;
            parameters[1].Value = loginInfo;
            parameters[2].Value = password;

            DataSet ds = DbHelperMySQL.Query(strSql.ToString(), parameters);
            if (ds != null && ds.Tables != null && ds.Tables.Count > 0 && ds.Tables[0].Rows != null && ds.Tables[0].Rows.Count > 0)
            {
                model = GetModelByDataRow(ds.Tables[0].Rows[0]);
            }
            if (model != null)
            {
                result = true;
            }

            return result;
        }
        #endregion ******************************Public Method******************************
    }
}
